﻿using FreeSql;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using Ynzp.DemoApi.Entities;

namespace FreeSQLTest
{
    /// <summary>
    /// Author: yus 
    /// Corporation: 
    /// Description: 
    /// DateTime: 2020/7/7 9:01:01
    /// </summary>
    public class Test1
    {
        public Test1()
        {
            fsql = BuildFSQL();
        }

        IFreeSql fsql;

        IFreeSql BuildFSQL()
        {
            string constr = "server=192.168.0.230; uid=yus;pwd=123456;database=yusdb";

            var fsql = new FreeSql.FreeSqlBuilder()
                              .UseConnectionString(FreeSql.DataType.SqlServer, constr)
                              .UseMonitorCommand(cmd => { }, (cmd, traceLog) =>
                              {
                                  Console.WriteLine($"{cmd.CommandText}\r\n");
                              })
                              .Build();

            fsql.Aop.TraceBefore += (s, e) =>
            {
                Console.WriteLine($"TraceBefore:{e.Operation}");
            };

            fsql.Aop.TraceAfter += (s, e) =>
            {
                Console.WriteLine($"TraceAfter:{e.Remark}");
            };

            return fsql;
        }

        public void TestUnitOfWorkManagerByRepo()
        {
            var uowm = new UnitOfWorkManager(fsql);

            var repo = new DefaultRepository<User,int>(fsql, uowm);

            using (var uow1 = uowm.Begin())
            {

                using (var uow2 = uowm.Begin())
                {
                    var newID = repo.Insert(new User()
                    {
                        Name = $"Name-{ DateTime.Now}"
                    }).Id;

                    Console.WriteLine($"NewID:{newID}");

                    uow2.Commit();
                }

                uow1.Commit();
            }

            var data = repo.Where(null).OrderByDescending(a => a.Id).First(); //是否变化

            Console.WriteLine($"AfterQuery:{JsonConvert.SerializeObject(data)}");
        }

        /// <summary>
        /// UnitOfWorkManager测试
        /// </summary>
        public void TestUnitOfWorkManagerByFSql()
        {
            var uowm = new UnitOfWorkManager(fsql);

            //var repo = new DefaultRepository<User, int>(fsql, uowm);

            using (var uow1 = uowm.Begin(Propagation.Required))
            {
                using (var uow2 = uowm.Begin(Propagation.Required))
                {
                    var newID = fsql.Insert<User>(new User()
                    {
                        Name = $"Name-{ DateTime.Now}"
                    }).ExecuteIdentity();

                    Console.WriteLine($"NewID:{newID}");

                    uow2.Commit();
                }

                uow1.Commit();
            }

            var data = fsql.Select<User>().Where(null).OrderByDescending(a => a.Id).First(); //是否变化

            Console.WriteLine($"AfterQuery:{JsonConvert.SerializeObject(data)}");
        }
    }
}
